package controllers;

import java.io.IOException;
import java.io.Serializable;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import dao.EmployeeDAORemote;
import dto.EmployeeDTO;
import emailbeans.EmployeeBean;

@ManagedBean(name = "authentificationController")
@SessionScoped
public class AuthentificationController implements Serializable {

	private static final long serialVersionUID = 1L;

	@EJB
	EmployeeDAORemote employeeDAO;

	@ManagedProperty(value = "#{employeeBean}")
	private EmployeeBean employeeBean;
	
	@ManagedProperty(value="#{menuController}")
	private MenuController menuController;

	@ManagedProperty(value = "#{employeeDTO}")
	EmployeeDTO employeeDTO;
	ExternalContext exCon;
	private int employeeId;

	// private String password;

	public void getEmployee() {

		EmployeeDTO eDTO = employeeDAO
				.findEmployeeByUsernameAndPassword(employeeDTO);
		if (eDTO != null) {
			employeeId = eDTO.getId();
			employeeDTO.setId(eDTO.getId());
			if (employeeDTO.getPassword().equals("1234")) {
				try {

					FacesContext
							.getCurrentInstance()
							.getExternalContext()
							.redirect(
									"/MSGCandidateTesting/faces/changePassword.xhtml");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			} else if (!(employeeDTO.getPassword().equals("1234"))
					&& (eDTO.getRank() == 1)) {
			
			System.out.println("ADMIN");
			employeeBean.setUsername(employeeDTO.getUsername());
			employeeBean.setPassword(employeeDTO.getPassword());
			employeeBean.setEmail(employeeDTO.getEmail());
			
			menuController.setUserRank(0);
			menuController.setLoggedIn(true);
			menuController.clearMenuItems();
			menuController.addMenuItem("editAdmin.xhtml", "Administrators");
			menuController.addMenuItem("alocateTesterUser.xhtml", "Testers");
			menuController.addMenuItem("editCandidates.xhtml", "Candidates");
			menuController.addMenuItem("editDomains.xhtml", "Domains - Categories");
			menuController.addMenuItem("editQuestions.xhtml", "Questions");
			menuController.addMenuItem("roomPage.xhtml", "Rooms");
			menuController.addMenuItem("logOut.xhtml", "Log out");
			menuController.setMeniuURL("editAdmin.xhtml");
			
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/MSGCandidateTesting/faces/editAdmin.xhtml");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			} else if (!(employeeDTO.getPassword().equals("1234"))
					&& (eDTO.getRank() == 2)) {
				try {

					employeeBean.setUsername(employeeDTO.getUsername());
					employeeBean.setPassword(employeeDTO.getPassword());
					employeeBean.setEmail(employeeDTO.getEmail());

					menuController.setUserRank(1);
					menuController.setLoggedIn(true);
					menuController.clearMenuItems();
					menuController.addMenuItem("testerhome.xhtml","Visualize users");
					menuController.addMenuItem("createTemplate.xhtml","Create template");
					menuController.addMenuItem("addQuestion.xhtml","Add questions");
					menuController.addMenuItem("logOut.xhtml","Logout");
					menuController.setMeniuURL("testerhome.xhtml");
					
					FacesContext
							.getCurrentInstance()
							.getExternalContext()
							.redirect(
									"/MSGCandidateTesting/faces/testerhome.xhtml");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}else 	if (eDTO == null) {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Please enter a valid username and password"));

		}
	}

	public void addEmployee(ActionEvent event) {
		try {
			employeeDTO = employeeDAO.getEmployeeByEmailAndUsername(employeeDTO
					.getEmail(), employeeDTO.getUsername());

			if (employeeDTO.getEmail() != null
					|| employeeDTO.getUsername() != null) {
				System.out.println("eroare");

			} else {
				employeeDAO.addEmployee(employeeDTO);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	
	
	public MenuController getMenuController() {
		return menuController;
	}

	public void setMenuController(MenuController menuController) {
		this.menuController = menuController;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	public int getEmployeeId() {
		return employeeId;
	}

	public EmployeeDTO getEmployeeDTO() {
		return employeeDTO;
	}

	public void setEmployeeDTO(EmployeeDTO employeeDTO) {
		this.employeeDTO = employeeDTO;
	}

	public EmployeeBean getEmployeeBean() {
		return employeeBean;
	}

	public void setEmployeeBean(EmployeeBean employeeBean) {
		this.employeeBean = employeeBean;
	}

}
